//
//  UIStandardNameInputLabel.m
//  vhECGTools
//
//  Created by jia yu on 2022/7/14.
//

#import "UIStandardNameInputLabel.h"

@interface UIStandardNameInputLabel(){
    float screenScale;
    float space;
}
@end

@implementation UIStandardNameInputLabel
@synthesize nameLabel;
@synthesize inputField;
@synthesize unitLabel;
@synthesize style;
@synthesize string;

-(void)refresh{
    inputField.text = string;
    switch (style) {
        case UIStandardNameInputLabel_Style_Default:{
            CGSize size = [nameLabel.text sizeWithAttributes:@{NSFontAttributeName:nameLabel.font}];
            [nameLabel setFrame:CGRectMake(0.f, 0.f, size.width + space, self.frame.size.height)];
            [inputField setFrame:CGRectMake(size.width+space, 0.f, self.frame.size.width-size.width-space, self.frame.size.height)];
            break;}
        case UIStandardNameInputLabel_Style_Manual:{
            CGSize nameSize = [nameLabel.text sizeWithAttributes:@{NSFontAttributeName:nameLabel.font}];
            CGSize unitSize = [unitLabel.text sizeWithAttributes:@{NSFontAttributeName:unitLabel.font}];
            [nameLabel setFrame:CGRectMake(0.f, 0.f, nameSize.width, self.frame.size.height)];
            [unitLabel setFrame:CGRectMake(self.frame.size.width-unitSize.width, 0.f, unitSize.width, self.frame.size.height)];
            break;}
        default:
            break;
    }
}

-(void)setString:(NSString *)_string{
    string = _string;
    [self refresh];
    return;
}

-(id)initWithFrame:(CGRect)frame scale:(float)scale{
    self = [super initWithFrame:frame];
    if (self){
        self.backgroundColor = [UIColor redColor];
        style = UIStandardNameInputLabel_Style_Default;
        screenScale = scale;
        space = 30.f * scale;
        self.backgroundColor = [UIColor clearColor];
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, frame.size.width/2.f, frame.size.height)];
        nameLabel.textColor = [vhECGTools colorFromHEXString:@"606972"];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.font = [UIFont fontWithName:@"PingFangSC-Thin" size:34.f * scale];
        nameLabel.text = @"Name:";
        [self addSubview:nameLabel];
        
        inputField = [[UIStandardInputTextField alloc] initWithFrame:CGRectMake(frame.size.width/2.f, 0.f, frame.size.width/2.f, frame.size.height) scale:screenScale];
        inputField.style = UIStandardInputTextField_Style_WithBoard;
        [self addSubview:inputField];
        
        unitLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, frame.size.width/2.f, frame.size.height)];
        unitLabel.textColor = [vhECGTools colorFromHEXString:@"606972"];
        unitLabel.backgroundColor = [UIColor clearColor];
        unitLabel.textAlignment = NSTextAlignmentCenter;
        unitLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:34.f * scale];
        unitLabel.text = @"";
        [self addSubview:unitLabel];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    NSLog(@"-(id)initWithFrame:(CGRect)frame sacle:(float)scale");
    return nil;
}

-(BOOL)resignFirstResponder{
    [inputField resignFirstResponder];
    [super resignFirstResponder];
    return YES;
}

-(void)drawRect:(CGRect)rect{
    [self refresh];
    [super drawRect:rect];
}
@end
